package com.gl.goods.ui.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.ImageView;

import com.gl.goods.R;
import com.gl.goods.base.BaseActivity;
import com.gl.goods.httpclint.HttpApi;
import com.gl.goods.httpclint.response.JsonResponseHandler;
import com.gl.goods.ui.login.bean.UserInfoBean;
import com.gl.library.utils.StatusBarUI;
import com.gl.library.utils.StringUtils;
import com.gl.library.widget.edittext.ClearEditText;
import com.gl.library.widget.smartloadview.SmartLoadingView;
import com.google.gson.Gson;
import com.hjq.toast.ToastUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity implements TextWatcher {

    @BindView(R.id.tv_register)
    SmartLoadingView login;
    @BindView(R.id.et_phone)
    ClearEditText et_phone;
    @BindView(R.id.et_password)
    ClearEditText et_password;
    @BindView(R.id.tv_password_again)
    ClearEditText tv_password_again;
    @BindView(R.id.iv_eye)
    ImageView mCheckPwdIv;
    /**输入框密码是否是隐藏的，true*/
    private boolean isHideFirst = true;
    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitleVisibility(true);
        StatusBarUI.setStatusBarTranslucent(mActivitySelf);

        et_phone.addTextChangedListener(this);
        et_password.addTextChangedListener(this);
        tv_password_again.addTextChangedListener(this);
    }

    @OnClick({R.id.tv_register,R.id.tv_back,R.id.iv_eye,R.id.ll_bottom})
    public void onClick(View view){
        Intent intent = new Intent();
        String phone = et_phone.getText().toString();
        String password = et_password.getText().toString();
        String passwordAgain = tv_password_again.getText().toString();
        switch (view.getId()){
            case R.id.tv_register:
                login.start();
                if (password.length() < 6){
                    ToastUtils.show("密码不得小于6位");
                    login.netFaile("注册");
                    login.setSmartClickable(true);
                    return;
                }
                if (password.length() > 15){
                    ToastUtils.show("密码不得大于15位");
                    login.netFaile("注册");
                    login.setSmartClickable(true);
                    return;
                }
                if (!StringUtils.equals(password,passwordAgain)){
                    ToastUtils.show("两次密码输入不一致");
                    login.netFaile("注册");
                    login.setSmartClickable(true);
                    return;
                }
                //注册
                HttpApi.register(phone, passwordAgain, new JsonResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, JSONObject response) {
                        super.onSuccess(statusCode, response);
                        hideDialogLoading();
                        Gson gson = new Gson();
                        UserInfoBean successBean = gson.fromJson(response.toString(), UserInfoBean.class);
                        if (StringUtils.isNotNull(successBean)){
                            UserInfoBean.DataDTO dataBean = successBean.getData();
                            if (StringUtils.isNotNull(dataBean)){
                                ToastUtils.show("注册成功");
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        ToastUtils.show(error_msg);
                        login.netFaile();
                    }
                });
                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.iv_eye:
                if (isHideFirst == true) {
                    mCheckPwdIv.setImageResource(R.mipmap.eye_open);
                    //明文
                    HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
                    et_password.setTransformationMethod(method1);
                    isHideFirst = false;
                }else {
                    mCheckPwdIv.setImageResource(R.mipmap.eye_close);
                    //密文
                    TransformationMethod method1 = PasswordTransformationMethod.getInstance();
                    et_password.setTransformationMethod(method1);
                    isHideFirst = true;
                }
                // 光标的位置
                int index = et_password.getText().toString().length();
                et_password.setSelection(index);
                break;
            case R.id.ll_bottom:
                /**
                 * 注册协议
                 */
                intent.putExtra("urlId","117");
//                goTo(TimiAgreementWebViewActivity.class,intent);
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(et_phone.getText().toString().trim()) && !TextUtils.isEmpty(et_password.getText().toString().trim())
                && !TextUtils.isEmpty(tv_password_again.getText().toString().trim())) {
            login.setSmartClickable(true);
        } else {
            login.setSmartClickable(false);
        }
    }
}
